package com.instalinks.android.smspostservice.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.instalinks.android.smspostservice.AppTabBaseActivity;
import com.instalinks.android.smspostservice.R;

public class UserConfigActivity extends AppTabBaseActivity {

	private RadioGroup rbgHostingType;
	private TextView lblUrlAutoResponse;
	private EditText txtUrlAutoResponse;
	private EditText txtFailAutoResponse;

	private CheckBox chkAllowGPRS;

	private CheckBox chkBattery;
	private CheckBox chkSignal;
	private CheckBox chkCounters;

	private Button btnApply;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.initialize();
	}

	private void initialize() {
		this.setContentView(R.layout.userconfig);


		lblUrlAutoResponse = (TextView) findViewById(R.id.uc_lblUrlAutoResponse);
		txtUrlAutoResponse = (EditText) findViewById(R.id.uc_txtUrlAutoResponse);
		txtFailAutoResponse = (EditText) findViewById(R.id.uc_txtFailureResponse);
		chkAllowGPRS = (CheckBox) findViewById(R.id.uc_chkAllowGPRS);

		chkBattery = (CheckBox) findViewById(R.id.uc_chkBattery);
		chkSignal = (CheckBox) findViewById(R.id.uc_chkSignal);
		chkCounters = (CheckBox) findViewById(R.id.uc_chkCounters);

		btnApply = (Button) findViewById(R.id.uc_btnApply);
		btnApply.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				saveConfigOnHost();
			}
		});

		rbgHostingType = (RadioGroup) findViewById(R.id.uc_rbgHostingType);
		rbgHostingType
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						switch (checkedId) {
						case R.id.uc_rbInstalinks:
							lblUrlAutoResponse
									.setText(getString(R.string.uc_lblurlautoresponse_instalinks));
							break;
						case R.id.uc_rbCustom:
							lblUrlAutoResponse
									.setText(getString(R.string.uc_lblurlautoresponse_custom));
							break;
						}
					}
				});

		loadConfigValues();

	}

	private void loadConfigValues() {
		mAppCfg.rePopulate();

		String destination = mAppCfg.getDestination();
		if (destination.toUpperCase().startsWith("HTTP://")
				|| destination.toUpperCase().startsWith("HTTPS://")) {
			rbgHostingType.check(R.id.uc_rbCustom);
			lblUrlAutoResponse
					.setText(getString(R.string.uc_lblurlautoresponse_custom));
		} else {
			rbgHostingType.check(R.id.uc_rbInstalinks);
			lblUrlAutoResponse
					.setText(getString(R.string.uc_lblurlautoresponse_instalinks));
		}

		txtUrlAutoResponse.setText(destination);
		txtFailAutoResponse.setText(mAppCfg.getFailReplyMsg());
		chkAllowGPRS.setChecked(mAppCfg.isGprsFlag());

		chkBattery.setChecked(mAppCfg.isBatteryFlag());
		chkSignal.setChecked(mAppCfg.isSignalFlag());
		chkCounters.setChecked(mAppCfg.isCountersFlag());
	}

	private void saveConfigOnHost() {
		// PERFORM VALIDATIONS

		if (!validateInput())
			return;

		// If VALID -- START THREAD
		UserConfigActivity.this.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				(new SubTask("Saving configuration...")).execute();
			}
		});
	}

	private boolean validateInput() {
		boolean result = false;

		int rbId = this.rbgHostingType.getCheckedRadioButtonId();
		String txt = this.txtUrlAutoResponse.getText().toString();

		if (rbId == R.id.uc_rbInstalinks
				&& (txt.toUpperCase().startsWith("HTTP://") || txt
						.toUpperCase().startsWith("HTTPS://"))) {

			mAppCfg.alert("Message should not start with http:// or https://");

		} else if (rbId == R.id.uc_rbCustom
				&& (!txt.toUpperCase().startsWith("HTTP://") && !txt
						.toUpperCase().startsWith("HTTPS://"))) {

			mAppCfg.alert("URL must start with http:// or https://");
		} else {
			result = true;
		}

		return result;
	}

	@Override
	protected boolean doBackgroundWork() {
		boolean result = mAppCfg.sendConfigToHost(txtUrlAutoResponse
				.getText().toString(), txtFailAutoResponse.getText()
				.toString(), chkBattery.isChecked(), chkSignal.isChecked(),
				chkCounters.isChecked(), chkAllowGPRS.isChecked());

		if (result) {
			
			try {
				mAppCfg.setServerNotificationFlag(true);
			} catch (Exception e) {
			}
		}
		return result;
	}

	@Override
	protected void doOnSuccess() {
		loadConfigValues();
		mAppCfg.alert("Configuration saved successfully");
	}

	@Override
	protected void doOnFailure() {
		mAppCfg.alert("Error saving configuration. Try again.");
	}
}
